"""
支出二级类模型
"""
from app.db import db  # 从独立的 db 模块导入


class ExpenseSecondCate(db.Model):
    # 定义表名
    __tablename__ = 'expense_second_cate'

    # 字段
    id = db.Column(db.Integer, primary_key=True)
    second_cate_name = db.Column(db.String(50), nullable=False)
    # 外键
    first_cate_id = db.Column(db.Integer, db.ForeignKey('expense_first_cate.id'))
    # 关联
    first_cate = db.relationship('ExpenseFirstCate', backref=db.backref('second_cates', lazy='dynamic'))

    def to_dict(self):
        """将模型实例转换为字典，方便后续处理（如转换为DataFrame或JSON）"""
        return {
            'id': self.id,
            'second_cate_name': self.second_cate_name
        }
